package com.example.sample.activity;

import com.emilsjolander.components.stickylistheaders.StickyListHeadersAdapter;
import com.emilsjolander.components.stickylistheaders.StickyListHeadersListView;
import com.example.app.BaseActivity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class StickyListViewActivity extends BaseActivity {
	
	// 黏性列表
	private StickyListHeadersListView mListView;
	
	final String[] mData = {
			"asfd", "aaaffffff", "bbbbbbbbbbbb", "cccccccccc",
			"asfd", "aaaffffff", "bbbbbbbbbbbb", "cccccccccc",
			"asfd", "aaaffffff", "bbbbbbbbbbbb", "cccccccccc",
			"asfd", "aaaffffff", "bbbbbbbbbbbb", "cccccccccc",
			"asfd", "aaaffffff", "bbbbbbbbbbbb", "cccccccccc",
			"asfd", "aaaffffff", "bbbbbbbbbbbb", "cccccccccc",
			"asfd", "aaaffffff", "bbbbbbbbbbbb", "cccccccccc",
			"ddddddddddd" 
			};
	final int mDataLen = mData.length;
	private int[] sectionIndices = new int [mDataLen];
	private Character[] sectionsLetters = new Character [mDataLen];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		sectionIndices = new int [mData.length];
		for (int i = 0; i < mData.length; i++) {
			sectionIndices[i] = i;
			sectionsLetters[i] = mData[i].charAt(0);		
		}
		
		mListView = new StickyListHeadersListView(this);
		mListView.setAdapter(new DemoListAdapter());
		setContentView(mListView);
	}
	
	private class DemoListAdapter extends BaseAdapter implements
			StickyListHeadersAdapter, SectionIndexer {

		@Override
		public int getCount() {
			return mData.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tv = null;
			if (convertView == null){
				tv = new TextView(StickyListViewActivity.this);
			}else{
				tv = (TextView)convertView;
			}
			
			String content = mData[position];
			tv.setText(content);
			return tv;
		}

		@Override
		public Object[] getSections() {
			return sectionsLetters;
		}

		@Override
		public int getPositionForSection(int section) {
			if (section >= sectionIndices.length) {
				section = sectionIndices.length - 1;
			} else if (section < 0) {
				section = 0;
			}
			return sectionIndices[section];
		}

		@Override
		public int getSectionForPosition(int position) {
			for (int i = 0; i < sectionIndices.length; i++) {
				if (position < sectionIndices[i]) {
					return i - 1;
				}
			}
			return sectionIndices.length - 1;
		}

		@Override
		public long getHeaderId(int position) {
			return mData[position].subSequence(0, 1).charAt(0);
		}

		@Override
		public View getHeaderView(int position, View convertView, ViewGroup parent) {
			TextView tv = null;
			if (convertView == null){
				tv = new TextView(StickyListViewActivity.this);
			}else{
				tv = (TextView)convertView;
			}			
			tv.setBackgroundColor(Color.rgb(200, 200, 200));
			
			Character content = sectionsLetters[position];
			String s = content.toString();
			tv.setText(s);
			return tv;
		}

	}
}
